package com.sat.common.utils.logger;

import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.DiskLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.orhanobut.logger.Printer;

/**
 * Created by Single on 2018/3/18.
 */

public class LoggerImpl extends AbsLogger {

    private String tag;
    private LoggerConfig config;

    public LoggerImpl(final LoggerConfig config) {
        super(config);

        this.config = config;

        Log.e("Chat","IsShow : "+ config.isShowThreadInfo());


        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(config.isShowThreadInfo())  // (Optional) Whether to show thread info or not. Default true
                .methodCount(config.getMethodCount())         // (Optional) How many method line to show. Default 2
                .methodOffset(config.getMethodOffset())        // (Optional) Hides internal method calls up to offset. Default 5
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return config.isLoggable();
            }
        });

        Logger.addLogAdapter(new DiskLogAdapter(){
            @Override
            public boolean isLoggable(int priority, @Nullable String tag) {
                return true;
            }
        });
    }

    @Override
    public ILogger tag(String tag) {
        this.tag = tag;
        return this;
    }

    @Override
    public void d(String message, Object... args) {
        getPrinter().d(message, args);
        tag = null;
    }

    @Override
    public void d(Object object) {
        getPrinter().d(object);
        tag = null;
    }

    @Override
    public void e(String message, Object... args) {
        getPrinter().e(message, args);
        tag = null;
    }

    @Override
    public void i(String message, Object... args) {
        getPrinter().i(message, args);
        tag = null;
    }

    @Override
    public void v(String message, Object... args) {
        getPrinter().v(message, args);
        tag = null;
    }

    @Override
    public void w(String message, Object... args) {
        getPrinter().w(message, args);
        tag = null;
    }

    @Override
    public void json(String json) {
        getPrinter().json(json);
        tag = null;
    }

    @Override
    public void xml(String xml) {
        getPrinter().xml(xml);
        tag = null;
    }

    private Printer getPrinter() {
        String tmpTag = TextUtils.isEmpty(tag) ? config.getAppTag() : tag;
        return Logger.t(tmpTag);
    }
}
